package org.viettan.android.main.view;

import java.util.List;

import org.viettan.android.main.R;
import org.viettan.android.main.model.RadioEpisode;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.google.inject.Inject;
import com.google.inject.Provider;

/**
 * This {@code RadioEpisodeAdapter} helps in display {@link RadioEpisode}s onto a list in the UI.
 */
public class RadioEpisodeAdapter extends ArrayAdapter<RadioEpisode> {
    private final List<RadioEpisode> items;
    private final Context context;
    private final ViewUtility viewUtility;

    @Inject
    RadioEpisodeAdapter(Provider<ViewUtility> viewUtilityProvider, Context context,
	    int textViewResourceId, List<RadioEpisode> items) {
	super(context, textViewResourceId, items);
	this.items = items;
	this.context = context;
	this.viewUtility = viewUtilityProvider.get();
    }

    @Override
    public View getView(int position, View rowView, ViewGroup parent) {
	if (rowView == null) {
	    LayoutInflater vi = (LayoutInflater) context
		    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    rowView = vi.inflate(R.layout.radio_row, null);
	}
	RadioEpisode radioEpisode = items.get(position);
	viewUtility.setText(rowView, R.id.title, radioEpisode.getTitle());
	viewUtility.setText(rowView, R.id.subtitle,
		String.format("[%s]", radioEpisode.getSubtitle()));
	viewUtility.setText(rowView, R.id.thirdline, radioEpisode.getAuthor());

	return rowView;
    }
}
